package org.glut.wechat.controller;
import org.glut.wechat.domain.Video;
import org.glut.wechat.service.IVideoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import lombok.Data;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/video")
public class VideoController {
    private static final Logger log = LoggerFactory.getLogger(VideoController.class);
    @Autowired
    private IVideoService videoService;

    @Data
    static class ApiResponse<T> {
        private int code;    // 200=成功，500=失败
        private String msg;  // 提示信息
        private T data;      // 业务数据（原逻辑不变）

        // 保留原构造器，新增无参构造器（@Data自动生成，或手动添加）
        public ApiResponse() {}
        public ApiResponse(int code, String msg, T data) {
            this.code = code;
            this.msg = msg;
            this.data = data;
        }
    }

    @GetMapping("/list")
    public ResponseEntity<ApiResponse<List<Video>>> getVideoList() {
        try {
            List<Video> videoList = videoService.getVideoList();
            log.info("获取视频列表成功，数量：{}", videoList.size());
            return ResponseEntity.ok(new ApiResponse<>(200, "获取成功", videoList));
        } catch (Exception e) {
            log.error("获取视频列表失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse<>(500, "获取失败：" + e.getMessage(), null));
        }
    }
    @PostMapping("/upload")
    public ResponseEntity<ApiResponse<String>> uploadVideo(
            @RequestParam("video") MultipartFile video,
            @RequestParam("title") String title,
            @RequestParam("description") String description) {
        try {
            // 调用服务层处理视频上传
            String result = videoService.uploadVideo(video, title, description);
            return ResponseEntity.ok(new ApiResponse<>(200, "上传成功", result));
        } catch (Exception e) {
            log.error("视频上传失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse<>(500, "上传失败：" + e.getMessage(), null));
        }
    }
    @GetMapping("/random")
    public ResponseEntity<ApiResponse<Video>> getRandomVideo(
            @RequestParam(required = false) String excludeIds) {
        try {
            List<Integer> excludeIdList = new ArrayList<>();
            if (excludeIds != null && !excludeIds.isEmpty()) {
                // 解析逗号分隔的ID字符串为整数列表
                String[] idStrs = excludeIds.split(",");
                for (String idStr : idStrs) {
                    try {
                        excludeIdList.add(Integer.parseInt(idStr.trim()));
                    } catch (NumberFormatException e) {
                        log.warn("无效的视频ID格式: {}", idStr);
                    }
                }
            }
            Video video = videoService.getRandomVideo(excludeIdList);
            if (video != null) {
                return ResponseEntity.ok(new ApiResponse<>(200, "获取成功", video));
            } else {
                return ResponseEntity.ok(new ApiResponse<>(200, "没有更多视频", null));
            }
        } catch (Exception e) {
            log.error("获取随机视频失败", e);
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
                    .body(new ApiResponse<>(500, "获取失败：" + e.getMessage(), null));
        }
    }
}